package com.arpa.ntocc.common.service.impl;

import cn.hutool.core.util.IdUtil;
import com.arpa.ntocc.common.common.util.CommonUtil;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.ntocc.common.domain.dto.BannerDTO;
import com.arpa.ntocc.common.domain.entity.Banner;
import com.arpa.ntocc.common.domain.enums.FileInfoType;
import com.arpa.ntocc.common.domain.vo.BannerVO;
import com.arpa.ntocc.common.mapper.BannerMapper;
import com.arpa.ntocc.common.service.IBannerService;
import com.arpa.ntocc.common.service.IFileInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 广告表 服务实现类
 * </p>
 *
 * @author LEO
 * @since 2019-12-25
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements IBannerService {
    @Autowired
    private IFileInfoService iFileInfoService;
    /**
     * 保存
     * @param entity
     * @return
     */
    @Override
    @Transactional(rollbackFor={Exception.class})
    public boolean save(Banner entity) {
        if(StringUtils.isBlank(entity.getCode())){
            entity.setCode(IdUtil.simpleUUID());
        }
        if(StringUtils.isBlank(entity.getCreatedBy())){
            entity.setCreatedBy(UserUtil.getCode());
        }
        entity.setBranchCode(UserUtil.getBranchCode());
        this.saveImg(entity);
        return super.save(entity);
    }

    /**
     * 根据code更新实体
     * @param entity
     * @return
     */
    @Override
    @Transactional(rollbackFor={Exception.class})
    public int updateByCode(Banner entity) {
        if(StringUtils.isBlank(entity.getModifiedBy())){
            entity.setModifiedBy(UserUtil.getCode());
        }
        this.saveImg(entity);
        return baseMapper.update(entity,new QueryWrapper<Banner>().lambda().eq(Banner::getCode,entity.getCode()));
    }
    private void saveImg(Banner entity){
        //保存图片
        if (!StringUtils.isBlank(entity.getImg())){
            iFileInfoService.saveList(entity.getCode(), FileInfoType.SITE_BANNER, entity.getImg());
        }
    }
    /**
     * 查询列表
     * @param bannerDTO
     * @return
     */
    @Override
    public List<BannerVO> queryList(BannerDTO bannerDTO){
        //排序字段名需要驼峰转数据库下划线类型字段名
        if(StringUtils.isNotEmpty(bannerDTO.getSortField())){
            bannerDTO.setSortField(CommonUtil.camel2Underline(bannerDTO.getSortField()));
        }
        List<BannerVO> bannerVOList = baseMapper.queryList(bannerDTO);
        return bannerVOList;
    }

    /**
     * 查询合计,包含总数
     * @param bannerDTO
     * @return
     */
    @Override
    public BannerVO queryListSum(BannerDTO bannerDTO){
        BannerVO bannerVO = baseMapper.queryListSum(bannerDTO);
        return bannerVO;
    }

}
